package com.jt.controller;

import com.jt.annotation.RequiredLog;
import com.jt.service.FileService;
import com.jt.vo.ImageVO;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    @RequiredLog
    public SysResult upload2(MultipartFile file){
        ImageVO imageVO = fileService.upload(file);
        if (imageVO == null){
            return SysResult.fail();
        }
        return SysResult.success(imageVO);
    }

    @DeleteMapping("/deleteFile")
    @RequiredLog
    public SysResult deleteFile(String virtualPath){

        fileService.deleteFile(virtualPath);
        return SysResult.success();
    }








    //@PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {
        //1.获取图片的名称
        String fileName = file.getOriginalFilename();
        //2.封装文件上传目录
        String fileDir = "E:/image";
        //3.检查目录是否存在
        File dir = new File(fileDir);
        if (!dir.exists())
            dir.mkdirs();
        //4.封装文件的全路径
        String path = fileDir + "/" +fileName;
        //5.上传文件
        file.transferTo(new File(path));
        return SysResult.success();

    }

}
